/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package phd.dupenois.changeidentifiers.application.graphical.frames;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JPanel;
import phd.dupenois.changeidentifiers.footprints.Footprint;
import uk.co.dupenois.geometry.Coordinate;

/**
 *
 * @author Max
 */
public class FootprintPanel extends JPanel{
    private Polygon footprintPolygon;
    private Color colour;
    private Color colourTranslucent;
    private Grid grid;
    private Color transparent = new Color(0, 0, 0, 0);
    public FootprintPanel(Color colour, Grid grid){
        super();
        this.setBackground(transparent);
        this.setOpaque(false);
        this.colour = colour;
        this.colourTranslucent = new Color(colour.getRed(), colour.getGreen(), colour.getBlue(), 100);
        this.grid = grid;
    }
    public void setFootprint(Footprint footprint){
        this.footprintPolygon = convertFootprintToPolygon(footprint);
    }

    private Polygon convertFootprintToPolygon(Footprint footprint){
        Coordinate[] coords = footprint.getCoordinates();
        Polygon polygon = new Polygon();
        Point point;
        for(Coordinate c : coords){
            point = grid.getScreenPosition(c);
            polygon.addPoint(point.x, this.getHeight()-point.y);
        }
        return polygon;
    }
    @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
//        g2.setColor(transparent);
//        g2.clearRect(0, 0, this.getWidth(), this.getHeight());
        if(this.footprintPolygon!=null){
            g2.setColor(colour);
            g2.drawPolygon(footprintPolygon);
            g2.setColor(colourTranslucent);
            g2.fillPolygon(footprintPolygon);
        }
    }
}
